(function(){
    var invite_add = {
    	init:function(){
            this.invite_insert();
        },
        invite_insert:function(){		/*新增物料*/
            var _this=this;
        	if($("#invite_insert").size() > 0){
        		$('form#invite_insert').submit(function(e) {

                    $("#invite_insert input.error").removeClass("error");

        			if($("form#invite_insert input[name=product_name]").val().length == 0){
        				$("form#invite_insert input[name=product_name]").addClass("error");
            			layer.msg("商品名称 必填", {icon:2,time:1000});
                        _this.scrollTp();
    			        return false;
            		}
                    var num = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;
            		if(!num.test($("input[name=price]").val())){
                        $("input[name=price]").addClass("error");
                        layer.msg("价格 请输入数字", {icon:2,time:1000});
                        _this.scrollTp();
                        return false;
                    }

            		if(!num.test($("input[name=one_num]").val())){ 
            			$("input[name=one_num]").addClass("error");
    			    	layer.msg("采购数量 请输入数字", {icon:2,time:1000});
                        _this.scrollTp();
    			        return false;
    			    }

                    if($(".xj-add-pic li").length==0){
                        $("input[name=upload_file]").addClass("error");
                        layer.msg("产品图片不能为空", {icon:2,time:1000});
                        _this.scrollTp();
                        return false;
                    }

            		var cu=confirm('发布后不能修改，确认发布？');
            		if(cu==false){
            			return false;
            		}
            		
        			$.ajax({
    			        type:"POST",
    			        dataType:"json",
    			        async: false,
                        url: get_baseUrl()+"Cmateriel/addPost",
                        data: $("form#invite_insert").serialize(),
                        beforeSend: function() {
                            $("#invite_insert input[type=submit]").val("正在提交...").attr("disabled", true);
                        },
    			        success: function(json) {
                            if (json.status) {
                            	layer.msg(json.errmsg, {icon:1,time:1000}, function(){
                            		window.location.href = json.data;
                            	});
                            } else {
                                $("#invite_insert input[type=submit]").val("确认发布").removeAttr("disabled");
                                layer.msg(json.errmsg, {icon:2,time:1000}, function(){
                                	if(json.data){window.location.href = json.data;}
                                });
                            }
                        }
    			    });
        			
                    e.preventDefault();
                });
        	}
        },

        scrollTp:function(){
            $(".add_ul").find(".error").each(function(index){
                $("html,body").stop().animate({scrollTop:$(this).offset().top-30},600,"easeInOutQuint");
                return false;
            });
        }
    };

    /*
    * 上传图片
    * */
    var upLoadImage={
        init:function(){
            this.str="";
            this.btn=$("#file-up1");
            this.content=$(".xj-add-pic ul");
            this.addPic();
            this.deletePic();
        },
        addPic:function(){
            var _this=this;
            this.btn.on("change",function(){
                var file = this.files[0];
                if (file.size > 5120 * 1024) {
                    layer.msg('图片不能超过5M', {icon: 2, time: 1000});
                    return;
                } else if (_this.content.children().length + 1 > 5) {
                	layer.msg("图片只能上传5张", {icon: 2, time: 1000});
                    return;
                }
                var src = window.URL.createObjectURL(file);
                $.ajaxFileUpload({
                    url: get_baseUrl()+'Cmateriel/file_add', //你处理上传文件的服务端
                    secureuri: false,
                    fileElementId:"file-up1",
                    data: {},
                    dataType: 'json',
                    success: function (data) {
                        if (data) {
                            str = '<li><img src="'+src+'" alt=""><span class="xj-delete2" data-fileid="'+data+'"></span></li>';
                            _this.content.prepend(str);
                            if(_this.content.children().length>0){
                                $(".add-send-btn span").text("继续上传");
                            }
                        } else {
                            layer.msg('上传失败', {icon: 2, time: 1000});
                        }
                    }
                })
            });
        },
        deletePic:function(){
            $(".xj-add-pic").on("click",".xj-delete2",function(){
                var this_id = $(this);
                $.post(get_baseUrl() + 'Cmateriel/file_delete', {id: this_id.data('fileid')}, function (json) {
                    if(json) {
                        this_id.parent('li').remove();
                        if(this_id.parents('ul').children().length==0){
                            $(".add-send-btn span").text("上传附件");
                        }
                    }
                }, 'json');
            });
        }
    };

    /*
    *地区
    * */
    var area={
        init:function(){
            this.area2();
        },
        area2:function(){  /*地区选择*/

            var arr2=[$("#r_area2 .select_area1"),$("#r_area2 .select_area2")];
            this.areaShow(arr2[0],arr2[1]);
        },
        areaShow:function(obj1,obj2){
            this.showBox(obj1);
            this.showBox(obj2);
            var $em=obj1.find("em");
            $em.on("click",function(){
                $(this).closest("div").hide();
                obj1.find("span").html($(this).text())    ;
                obj1.find("input").val($(this).text());
                obj2.find("p").hide().eq($(this).index()).show();
                obj2.find("span").html("请选择");
                obj2.find("input").val("请选择");
            });

            var $em2=obj2.find("em");

            $em2.on("click",function(){
                obj2.find("span").html($(this).text());
                obj2.find("input").val($(this).text());
                $(this).closest("div").hide();
            });
        },
        showBox:function(obj){
            obj.on("click","span",function(){
                $(this).parent().find(".select_list").toggle().end().siblings().find(".select_list").hide();
            });
        }
    };


    invite_add.init();
    area.init();
    upLoadImage.init();

})();